﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace NEWSPAPERINFOMATION
{
    public partial class From1 : Form
    {
        DataTable dt = new DataTable();
        int Noposition = 0;
        int datanum = 0;
        String jb;   //登录的用户的身份，user或admin
        String whoid;//登录的用户的id
        int type;
        public From1(String whojibie,String whoid,int caozuo)
        {
            InitializeComponent();
            jb = whojibie;
            this.whoid = whoid;
            this.type = caozuo;
           
            //种类选择框
            string sql = "SELECT * FROM newspaper.Class";
            BookConnection db = new BookConnection();
            dt = db.runSql(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string name = dt.Rows[i]["classname"].ToString();
                    this.comboBox1.Items.Add(name);
                }
                this.comboBox1.Items.Add("全部");
            }

           
            if (this.type == 1)
            {
                this.button_book.Visible = false;
                this.button_top.Visible = false;
                this.button_collect.Visible = false;
                this.button_update.Visible = false;
                this.button_delete.Visible = false;
                this.button_execute.Visible = false;
                this.button_execute.Visible = true;
                this.button_add.Visible = false;
                this.button1.Visible = false;
            }
            else if (this.type == 3)
            {
                this.button_book.Visible = true;
                this.button_top.Visible = false;
                this.button_update.Visible = false;
                this.button_delete.Visible = false;
                this.button_execute.Visible = false;
                this.button_execute.Visible = true;
                this.button_add.Visible = false;
                this.button1.Visible = false;
            }
            else if (this.type == 5)
            {
                this.button_collect.Visible = true;
                this.button_top.Visible = false;
                this.button_update.Visible = false;
                this.button_delete.Visible = false;
                this.button_execute.Visible = false;
                this.button_execute.Visible = true;
                this.button_add.Visible = false;
                this.button1.Visible = false;
            }
            else if (this.type == 2)
            {
                new 用户注册().Show();
                this.button_top.Visible = false;
                this.button_update.Visible = false;
                this.button_delete.Visible = false;
                this.button_execute.Visible = false;
                this.button_execute.Visible = true;
                this.button_add.Visible = false;
                this.button1.Visible = false;
                
            }
            else if (this.type == 4)
            {
                this.button_top.Visible = true;
                this.button_update.Visible = false;
                this.button_delete.Visible = false;
                this.button_execute.Visible = false;
                this.button_execute.Visible = true;
                this.button_add.Visible = false;
                this.button1.Visible = false;
                this.button_book.Visible = false;
                this.button_collect.Visible = false;
            }
            else if (this.type == 7)
            {

                this.button_book.Visible = false;
                this.button_collect.Visible = false;
                this.button1.Visible = false;
                this.button_update.Visible = true;
                this.button_delete.Visible = true;
                this.button_execute.Visible = true;
                this.button_execute.Visible = true;
                this.button_add.Visible = true;
            }
            else
            {
                this.Close();
            }
            if (jb.Equals("user"))
            {
                this.button_update.Visible = false;
                this.button_delete.Visible = false;
                this.button_execute.Visible = false;
                this.button_execute.Visible = true;
                this.button_add.Visible = false;
                this.button1.Visible = false;
            }
            else if (jb.Equals("admin"))
            {
                this.button_book.Visible = false;
                this.button_collect.Visible = false;

            }

        }
        //数据显示
        private void newspaper_show(DataTable dt)
        {
            this.id.Text = dt.Rows[Noposition]["newspaperid"].ToString();
            this.name.Text = dt.Rows[Noposition]["newspapername"].ToString();
            this.frequencyshow.Text = dt.Rows[Noposition]["frequency"].ToString();
            this.publishshow.Text = dt.Rows[Noposition]["publish"].ToString();
            this.priceshow.Text = dt.Rows[Noposition]["price"].ToString(); ;
            this.introductionshow.Text = dt.Rows[Noposition]["introduction"].ToString();
        }
        private void From1_Load(object sender, EventArgs e)
        {

            String sql = "select * from newspaper.Newspaper";
            BookConnection db = new BookConnection();
            dt = db.runSql(sql);  //返回查询结果

            if (dt.Rows.Count > 0)
            {
                datanum = dt.Rows.Count;
                newspaper_show(dt);
            }
            this.button_execute.Visible = false;

        }

        private void button_first_Click(object sender, EventArgs e)
        {
            if (Noposition == 0)
            {
                MessageBox.Show("已经是首条！");
                return;

            }
            Noposition = 0;
            newspaper_show(dt);
        }

        private void button_previous_Click(object sender, EventArgs e)
        {
            if (Noposition > 0)
            {
                Noposition--;
                newspaper_show(dt);
            }
            else
            {
                MessageBox.Show("已经是首条！");
            }
        }

        private void button_next_Click(object sender, EventArgs e)
        {
            if (Noposition != datanum - 1)
            {
                Noposition++;
                newspaper_show(dt);
            }
            else
            {
                MessageBox.Show("已经是末条！");
            }
        }

        private void button_last_Click(object sender, EventArgs e)
        {
            if (Noposition == datanum - 1)
            {
                MessageBox.Show("已经是末条!");
                return;
            }
            else
            {
                Noposition = datanum - 1;
                newspaper_show(dt);
            }
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            this.comboBox1.Visible = false;
            this.label8.Visible = false;
            this.button_top.Visible = false;
            this.button_book.Visible = false;
            this.button1.Visible = false;
            if (this.button_add.Text.Equals("增加"))
            {

                this.id.Text = "";
                this.name.Text = "";
                this.publishshow.Text = "";
                this.frequencyshow.Text = "";
                this.priceshow.Text = "";
                this.introductionshow.Text = "";

                //控件不可用
                this.button_first.Enabled = false;
                this.button_previous.Enabled = false;
                this.button_next.Enabled = false;
                this.button_last.Enabled = false;
                this.button_book.Enabled = false;
                this.button_update.Enabled = false;
                this.button_delete.Enabled = false;
                this.button_execute.Visible = true;
                this.button_execute.Enabled = true;
                this.button_add.Text = "取消";

            }
            else if (this.button_add.Text.Equals("取消"))
            {
                Noposition = 0;
                newspaper_show(dt);

                //控件可用
                this.button_first.Enabled = true;
                this.button_previous.Enabled = true;
                this.button_next.Enabled = true;
                this.button_last.Enabled = true;
                this.button_book.Enabled = true;
                this.button_update.Enabled = true;
                this.button_delete.Enabled = true;
                this.button_execute.Visible = true;
                this.button_execute.Enabled = false;
                this.button_add.Text = "增加";

            }
        }
        private void button_update_Click(object sender, EventArgs e)
        {
            this.comboBox1.Visible = false;
            this.label8.Visible = false;
            this.button_top.Visible = false;
            this.button_book.Visible = false;
            this.button1.Visible = false;
            if (this.button_update.Text.Equals("修改"))
            {
                //报刊号不可修改
                this.id.Enabled = false;
  
                //控件不可用
                this.button_first.Enabled = false;
                this.button_previous.Enabled = false;
                this.button_next.Enabled = false;
                this.button_last.Enabled = false;
                this.button_book.Enabled = false;
                this.button_add.Enabled = false;
                this.button_delete.Enabled = false;
                this.button_execute.Visible = true;
                this.button_execute.Enabled = true;
                this.button_update.Text="取消";

            }
            else if (this.button_update.Text.Equals("取消"))
            {
                Noposition = 0;
                newspaper_show(dt);

                //控件可用
                this.button_first.Enabled = true;
                this.button_previous.Enabled = true;
                this.button_next.Enabled = true;
                this.button_last.Enabled = true;
                this.button_book.Enabled = true;
                this.button_add.Enabled = true;
                this.button_delete.Enabled = true;
                this.button_execute.Visible = true;
                this.button_execute.Enabled = false;
                this.button_update.Text = "修改";

            }
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            this.comboBox1.Visible = false;
            this.label8.Visible = false;
            this.button_top.Visible = false;
            this.button_book.Visible = false;
            this.button1.Visible = false;
            if (this.button_delete.Text.Equals("删除"))
            {
                
                //控件不可用
                this.button_first.Enabled = false;
                this.button_previous.Enabled = false;
                this.button_next.Enabled = false;
                this.button_last.Enabled = false;
                this.button_book.Enabled = false;
                this.button_add.Enabled = false;
                this.button_update.Enabled = false;
                this.button_execute.Visible = true;
                this.button_execute.Enabled = true;
                this.button_delete.Text = "取消";

            }
            else if (this.button_delete.Text.Equals("取消"))
            {
                Noposition = 0;
                newspaper_show(dt);

                //控件可用
                this.button_first.Enabled = true;
                this.button_previous.Enabled = true;
                this.button_next.Enabled = true;
                this.button_last.Enabled = true;
                this.button_book.Enabled = true;
                this.button_add.Enabled = true;
                this.button_update.Enabled = true;
                this.button_execute.Visible = true;
                this.button_execute.Enabled = false;
                this.button_delete.Text = "删除";

            }
        }
        private void button_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_book_Click(object sender, EventArgs e)
        {
            new Book(this.id.Text.ToString(),whoid).Show();
        }

        private void button_execute_Click(object sender, EventArgs e)
        {
            if (this.button_add.Enabled)
            {
                string id = this.id.Text.ToString();
                string name = this.name.Text.Trim();
                string frequency = this.frequencyshow.Text.Trim();
                string publish = this.publishshow.Text.Trim();
                string price = this.priceshow.Text.Trim();
                string introduction = this.introductionshow.Text.Trim();
                if (id.Equals(""))
                {
                    MessageBox.Show("报刊号不为空！");
                    return;
                }
                if (name.Equals(""))
                {
                    MessageBox.Show("报刊名不为空！");
                    return;
                }
                if (introduction.Equals(""))
                {
                    MessageBox.Show("报刊名不为空！");
                    return;
                }
                float jg = 0;
                jg = float.Parse(price);//价格转为浮点型
                //首先根据报刊号查询是否有这样的记录
                BookConnection db1 = new BookConnection();
                string sql1 = "select * from newspaper.Newspaper where newspaperid='" + id + "'"; 
                int i = 0;
                i = db1.runSqlInt(sql1);
                if (i > 0)
                {
                    MessageBox.Show("报刊号已有！");
                    return;
                }
                //进行插入记录操作
                string sql2 = "insert into newspaper.Newspaper(newspaperid,newspapername,frequency,publish,price,introduction) values('" + id + "','" + name + "','" + frequency + "','" + publish + "','" + jg + "','" + introduction + "')";
                int j = db1.executeSqlResult(sql2);
                if (j > 0)
                {
                    MessageBox.Show("添加成功！！");
                    //刷新datatable
                    string sql21 = "select * from newspaper.Newspaper";
                    dt = db1.runSql(sql21);
                    if (dt.Rows.Count > 0)
                    {
                        Noposition = 0;
                        datanum = dt.Rows.Count;
                        newspaper_show(dt);

                        //控件可用
                        this.button_first.Enabled = true;
                        this.button_previous.Enabled = true;
                        this.button_next.Enabled = true;
                        this.button_last.Enabled = true;
                        this.button_book.Enabled = true;
                        this.button_add.Enabled = true;
                        this.button_update.Enabled = true;
                        this.button_delete.Enabled = true;
                        this.button_execute.Visible = true;
                        this.button_execute.Enabled = false;
                        this.button_add.Text = "增加";
                        this.comboBox1.Visible = true;
                        this.label8.Visible = true;
                        this.button_top.Visible = true;
                        this.button1.Visible = true;
                    }
                }
                else
                {
                    MessageBox.Show("添加失败！！");
                    return;
                }
            }
            else if (this.button_update.Enabled)
            {
                //修改
                string id = this.id.Text.ToString();
                string name = this.name.Text.Trim();
                string frequency = this.frequencyshow.Text.Trim();
                string publish = this.publishshow.Text.Trim();
                string price = this.priceshow.Text.Trim();
                string introduction = this.introductionshow.Text.Trim();
                if (id.Equals(""))
                {
                    MessageBox.Show("报刊号不为空！");
                    return;
                }
                if (name.Equals(""))
                {
                    MessageBox.Show("报刊名不为空！");
                    return;
                }
                if (introduction.Equals(""))
                {
                    MessageBox.Show("报刊名不为空！");
                    return;
                }
                float jg = 0;
                jg = float.Parse(price);//age转为整型
                if (MessageBox.Show("您真的要更新吗？", "请确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string sql3 = "update newspaper.Newspaper set newspapername='" + name + "',frequency='" + frequency + "',publish='" + publish + "',price='" + jg + "', introduction='" + introduction + "' where newspaperid='" + id + "'";
                    BookConnection db1 = new BookConnection();
                    int m = db1.executeSqlResult(sql3);
                    if (m > 0)
                    {
                        MessageBox.Show("更新成功！");
                        //重新绑定新的datatable
                        string sql31 = "select * from newspaper.Newspaper";
                        dt = db1.runSql(sql31);
                        if (dt.Rows.Count > 0)
                        {
                            Noposition = 0;
                            datanum = dt.Rows.Count;
                            newspaper_show(dt);
                            this.button_first.Enabled = true;
                            this.button_previous.Enabled = true;
                            this.button_next.Enabled = true;
                            this.button_last.Enabled = true;
                            this.button_book.Enabled = true;
                            this.button_add.Enabled = true;
                            this.button_update.Enabled = true;
                            this.button_delete.Enabled = true;
                            this.button_execute.Visible = true;
                            this.button_execute.Enabled = false;
                            this.button_update.Text = "修改";
                            this.comboBox1.Visible = true;
                            this.label8.Visible = true;
                            this.button_top.Visible = true;
                            this.button1.Visible = true;
                        }
                    }
                    else
                    {
                        MessageBox.Show("更新失败！");
                        return;
                    }
                }

                

                }
            else if (this.button_delete.Enabled)
            {
                //删除
                if (MessageBox.Show("您真的要删除吗？", "此删除不可恢复", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string baokanhao = this.id.Text.Trim();
                    string sql4 = "delete from newspaper.Newspaper where newspaperid='" + baokanhao + "'";
                    BookConnection db1 = new BookConnection();
                    int m = db1.executeSqlResult(sql4);
                    if (m > 0)
                    {
                        MessageBox.Show("删除成功！");
                        string sql41 = "select * from newspaper.Newspaper";
                        dt = db1.runSql(sql41);
                        if (dt.Rows.Count > 0)
                        {
                            Noposition = 0;
                            datanum = dt.Rows.Count;
                            newspaper_show(dt);
                            this.button_first.Enabled = true;
                            this.button_previous.Enabled = true;
                            this.button_next.Enabled = true;
                            this.button_last.Enabled = true;
                            this.button_book.Enabled = true;
                            this.button_add.Enabled = true;
                            this.button_update.Enabled = true;
                            this.button_delete.Enabled = true;
                            this.button_execute.Visible = true;
                            this.button_execute.Enabled = false;
                            this.button_delete.Text = "删除";
                            this.comboBox1.Visible = true;
                            this.label8.Visible = true;
                            this.button_top.Visible = true;
                            this.button1.Visible = true;

                        }
                    }
                    else
                    {
                        MessageBox.Show("删除失败！");
                        return;
                    }
                }
            }


        }

        private void button_collect_Click(object sender, EventArgs e)
        {
            BookConnection db=new BookConnection();
            string sql1 = "select * from users.Collection where newspaperid='" + this.id.Text.Trim()+ "' AND customid='"+whoid.Trim()+"'";
            int i = 0;
            i = db.runSqlInt(sql1);
            if (i > 0)
            {
                MessageBox.Show("此报刊已收藏，等待您赶快带回家哦！");
                return;
            }
              string sql = "INSERT INTO users.Collection VALUES('" + whoid + "','" + this.id.Text + "')";
             int j = db.executeSqlResult(sql);
             if (j > 0)
             {
                 MessageBox.Show("收藏成功！！");
             }
        }

        private void button_top_Click(object sender, EventArgs e)
        {
            new Sales().Show();
        }

        private void button_collection_Click(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new 用户注册().Show();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string kind = comboBox1.SelectedItem.ToString();
            BookConnection db = new BookConnection();
            if (kind.Equals("全部"))
            {
                String sql1 = "select Newspaper.*  FROM newspaper.Newspaper join newspaper.Newspaper_class ON newspaper.Newspaper.newspaperid=newspaper.Newspaper_class.newspaperid";
                dt =db.runSql(sql1);
            }
            else
            {
                String sql= "select Newspaper.*  FROM newspaper.Newspaper join newspaper.Newspaper_class ON newspaper.Newspaper.newspaperid=newspaper.Newspaper_class.newspaperid WHERE newspaper.Newspaper_class.classid  IN (SELECT classid  FROM newspaper.Class where classname='" + kind + "')";
                dt = db.runSql(sql);  //返回查询结果
            }
            if (dt.Rows.Count > 0)
            {
                Noposition = 0;
                datanum = dt.Rows.Count;
                newspaper_show(dt);
            }
            else if (dt.Rows.Count == 0)
            {
                MessageBox.Show("亲爱的顾客：很抱歉，暂时没有此类别的报刊！\n请尝试看看其他类别");
                return;
            }
            this.button_execute.Visible = false;
        }

     

   
    

      
    }
}